home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / AOCE Sample Code / PowerTalk Access Modules / Sample SMSAM / SampleSMSAM Source / BuildingBlocks / Queues.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-28  |  3.3 KB  |  116 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        Queues.h
  3.  
  4.     Copyright:    © 1991-1994 by Apple Computer, Inc.
  5.                 All rights reserved.
  6.  
  7.     Part of the AOCE Sample SMSAM Package.  Consult the license
  8.     which came with this software for your specific legal rights.
  9.  
  10. */
  11.  
  12.  
  13.  
  14. #pragma once
  15.  
  16. #ifndef __QUEUES__
  17. #define __QUEUES__
  18.  
  19. #ifndef    __TYPES__
  20. #include "Types.h"
  21. #endif
  22.  
  23. #ifndef    __DIRECTOBJECT__
  24. #include "DirectObject.h"
  25. #endif
  26.  
  27. /***********************************|****************************************/
  28.  
  29. #pragma push
  30. #pragma segment Queue
  31.  
  32. /***********************************|****************************************/
  33.  
  34. class ostream;
  35. class TQueueItem;
  36.  
  37. /***********************************|****************************************/
  38.  
  39. class TQueue : public TDirectObject
  40. {
  41. public:
  42.     virtual ~TQueue ();
  43.  
  44.             void*                     operator [] ( unsigned long ) const;
  45.             void*                    Get ( unsigned long ) const;
  46.  
  47.             void                     Append ( void* );                    // throws on memFullErr
  48.             void                     Insert ( unsigned long, void* );    // throws on memFullErr
  49.  
  50.             Boolean                 Remove ( const void* );        // true if obj was found
  51.             void*                     Remove ( unsigned long index = 1);    // returns obj
  52.             void                    RemoveAll ();
  53.  
  54.             Boolean                 Delete ( unsigned long );    // true if obj was found
  55.             Boolean                 Delete ( void* );        // true if obj was found
  56.             void                    DeleteAll ();
  57.  
  58.             unsigned long            Count () const;
  59.             unsigned long             Find ( const void* ) const;
  60.             Boolean                 IsValidIndex ( unsigned long ) const;
  61.  
  62.             void                    SetOwnsObjects ( Boolean = true );
  63.             Boolean                    GetOwnsObjects () const;
  64.  
  65.     virtual    ostream&                operator >> ( ostream& ) const;
  66.  
  67. protected:    TQueue ( Boolean ownsObjects = true );
  68.             void                    SetSize ( unsigned long objects );
  69.     virtual    void                    DeleteObject ( void* ) const;
  70.  
  71.             //    These methods manage the queue 'items' -- the objects which make up
  72.             //    the linked list that is the queue.
  73.             TQueueItem*                NewQueueItem ( void * objectToQueue);
  74.             void *                    ReturnQueueItem ( TQueueItem * queueItem );
  75.             
  76.             TQueueItem *            GetQueueItem ( unsigned long index ) const;
  77.             
  78.             
  79. private:    TQueue ( const TQueue& );
  80.             TQueue&            operator = ( const TQueue& );
  81.  
  82.             TQueueItem                *fFirst;
  83.             TQueueItem                *fLast;
  84.             
  85.             TQueueItem                *fSpareQueueItems;
  86.             
  87.             unsigned long             fCount;
  88.             Boolean                    fOwnsObjects;
  89. };
  90.  
  91. /***********************************|****************************************/
  92.  
  93. inline unsigned long TQueue::Count()const{return fCount;}
  94. inline Boolean TQueue::IsValidIndex(unsigned long i)const{return i>=1 && i<=fCount;}
  95. inline ostream& operator<<(ostream& s,const TQueue& l) {return l>>s;}
  96.  
  97. #define    DeclareQueue(OBJECT,QUEUE)\
  98.     class QUEUE:public TQueue{public:QUEUE();virtual ~QUEUE();\
  99.     Boolean Remove(const OBJECT* o){return TQueue::Remove((void*) o);};\
  100.     OBJECT* Remove(unsigned long index = 1){return (OBJECT*)TQueue::Remove(index);};\
  101.     OBJECT* operator[](unsigned long i)const{return (OBJECT*)TQueue::operator[](i);};\
  102.     OBJECT* Get(unsigned long i)const{return (OBJECT*)TQueue::Get(i);};\
  103.     virtual    ostream& operator>>(ostream&) const;\
  104.     protected: virtual void DeleteObject(void* o) const;}
  105.  
  106. #define    ImplementQueue(OBJECT,QUEUE,OWNS)\
  107.     QUEUE::QUEUE():TQueue(OWNS){}QUEUE::~QUEUE(){if(GetOwnsObjects())DeleteAll();}\
  108.     void QUEUE::DeleteObject(void* o)const{delete(OBJECT*)o;}\
  109.     ostream& QUEUE::operator>>(ostream& s)const{s<<#QUEUE;return TQueue::operator>>(s);}
  110.  
  111. /***********************************|****************************************/
  112.  
  113. #pragma pop
  114.  
  115. #endif
  116.